package top.yokey.android.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.widget.ContentLoadingProgressBar;

import top.yokey.android.R;
import top.yokey.android.base.BaseActivity;

public class WebActivity extends BaseActivity {

    private AppCompatImageView backImageView;
    private AppCompatImageView forwardImageView;
    private AppCompatImageView refreshImageView;
    private AppCompatImageView goImageView;
    private AppCompatEditText urlEditText;
    private WebView mainWebView;
    private ContentLoadingProgressBar mainProgressBar;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_web);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {

        backImageView = findViewById(R.id.backImageView);
        forwardImageView = findViewById(R.id.forwardImageView);
        refreshImageView = findViewById(R.id.refreshImageView);
        goImageView = findViewById(R.id.goImageView);
        urlEditText = findViewById(R.id.urlEditText);
        mainWebView = findViewById(R.id.mainWebView);
        mainProgressBar = findViewById(R.id.mainProgressBar);
        //加载一个默认的url地址
        mainProgressBar.setMax(100);
        mainWebView.loadUrl("https://www.baidu.com/");
        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);//开启javascipt支持
        webSettings.setUseWideViewPort(true);//开启界面的缩放
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);//设置缓存加载方式
        webSettings.setAllowFileAccess(true);//设置是否允许webview访问文件
        webSettings.setBlockNetworkImage(false);//是否屏蔽网络图片

    }

    @Override
    public void initEvent() {

        backImageView.setOnClickListener(view -> {
            if (mainWebView.canGoBack()) mainWebView.goBack();
        });

        forwardImageView.setOnClickListener(view -> {
            if (mainWebView.canGoForward()) mainWebView.goForward();
        });

        refreshImageView.setOnClickListener(view -> {
            mainWebView.reload();
        });

        goImageView.setOnClickListener(view -> {
            String url = urlEditText.getText().toString();
            if (url.isEmpty()) return;
            System.out.println(url);
            if (url.contains("https://") || url.contains("http://")) {
                mainWebView.loadUrl(url);
            } else {
                mainWebView.loadUrl("https://" + url);
            }
        });

        //为 webview 设置一个默认的webviewclient，去处理某些应该处理的事件
        mainWebView.setWebViewClient(new WebViewClient() {
            //设置使用webview处理Url加载事件
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                mainWebView.loadUrl(request.getUrl().toString());
                return true;
            }
        });

        mainWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                System.out.println(newProgress);
                if (newProgress < 100) mainProgressBar.setVisibility(View.VISIBLE);
                if (newProgress >= 100) mainProgressBar.setVisibility(View.GONE);
                mainProgressBar.setProgress(newProgress);
            }
        });

    }

}
